import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import locale from 'dayjs/esm/locale/zh-cn'
import tz from 'dayjs/plugin/timezone'
import utc from 'dayjs/plugin/utc'

dayjs.locale(locale)
dayjs.extend(relativeTime)
dayjs.extend(tz)
dayjs.extend(utc)

dayjs.tz.setDefault('Asia/Shanghai') // 设置中国时区

interface outputData {
  // 将时间点转化成距离现在的时间差
  formatTimeFromNow: (time: string) => string
  formatTime: (time: string, format: string) => string
}

export function useTimeFormat(): outputData {
  const formatTimeFromNow = (time: string | number): string => {
    if (!time || time === '-')
      return '-'
    return dayjs(time).fromNow() // .format('YYYY-MM-DD HH:mm:ss');
  }
  const formatTime = (time: string, format: string): string => {
    if (!time || time === '-')
      return '-'
    return dayjs(time).format(format)
  }
  return {
    formatTimeFromNow,
    formatTime,
  }
}
